package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.Bean.ChoiceBean;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.presentor.BankTypeProtocol;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.view.ChoiceDialog;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.net.protocol.AddBankInfoProtocol;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/30.
 * 描述：
 */

public class AddBankCardTwoActivity extends BaseActivity implements ChoiceDialog.OnClik {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.ll_bank_type)
    LinearLayout llBankType;
    @BindView(R.id.edit_bank_name)
    EditText editBankName;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.txt_bank)
    TextView txtBank;
    private BankTypeProtocol bankTypeProtocol;
    private ChoiceDialog choiceDialog;
    private String bankType = "-1";
    private String name;
    private String cardID;
    private String bankName;
    private String phone;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
        choiceDialog = new ChoiceDialog(this);
        choiceDialog.setOnClik(this);
        name = getIntent().getStringExtra("name");
        cardID = getIntent().getStringExtra("cardID");
    }

    @Override
    protected void responseData(GsonBaseProtocol mGsonBaseProtocal) {

    }

    @Override
    protected HttpLoader initHttpLoader() {
        return null;
    }

    @Override
    protected int initContentView() {
        return R.layout.add_bankcard_two_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {
        if (baseEventBean.code == Constants.TYPE_3) {
            finish();
        }
        return false;
    }

    private void initHead() {
        headerview.setTitle("填写银行卡信息");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @OnClick({R.id.ll_bank_type, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_bank_type:
                getBank();
                break;
            case R.id.btn_next:
                bankName = editBankName.getText().toString().trim();
                if (TextUtils.isEmpty(bankName)) {
                    ToastUtil.showToast("请输入支行名称");
                    return;
                }
                phone = editPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("银行预留手机号");
                    return;
                }
                if (bankType.equals("-1")) {
                    ToastUtil.showToast("请选择银行卡类型");
                    return;
                }
                addBankCard();
                break;
        }
    }

    private void addBankCard() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(AddBankInfoProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        map.put("user_name", name);
        map.put("card_num", cardID);
        map.put("bank_id", bankType);
        map.put("bank_branch", bankName);
        map.put("user_tel", phone);
        requestHttpLoader.setParams(NetConfig.ADD_CARD, map);
        requestHttpLoader.showDialog(this, true, "正在提交");
        requestHttpLoader.executor(new ResultListener<AddBankInfoProtocol>() {
            @Override
            public void success(AddBankInfoProtocol mGsonBaseProtocol) {
                Intent intent = new Intent(AddBankCardTwoActivity.this, VerificationPhoneActivity.class);
                intent.putExtra("phone", phone);
                intent.putExtra("id", mGsonBaseProtocol.getData().getCard_id());
                startActivity(intent);
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    private void getBank() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(BankTypeProtocol.class);
        Map<String, String> map = new HashMap<>();
        requestHttpLoader.setParams(NetConfig.BANK_TYPE, map);
        requestHttpLoader.showDialog(this, true);
        requestHttpLoader.executor(new ResultListener<BankTypeProtocol>() {
            @Override
            public void success(BankTypeProtocol mGsonBaseProtocol) {
                AddBankCardTwoActivity.this.bankTypeProtocol = mGsonBaseProtocol;
                List<ChoiceBean> list = new ArrayList<ChoiceBean>();
                for (BankTypeProtocol.DataBean bean : mGsonBaseProtocol.getData()) {
                    ChoiceBean choiceBean = new ChoiceBean();
                    choiceBean.name = bean.getBank_name();
                    list.add(choiceBean);
                }
                choiceDialog.setData(list);
                choiceDialog.show();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    @Override
    public void onClik(int position) {
        bankType = bankTypeProtocol.getData().get(position).getBank_id();
        txtBank.setText(bankTypeProtocol.getData().get(position).getBank_name());
    }
}
